package com.tencent.miniproject.presenter;

import android.util.Log;

import com.tencent.miniproject.been.DetailBeen;
import com.tencent.miniproject.constants.Constant;
import com.tencent.miniproject.presenter.i.IDetailPresenter;
import com.tencent.miniproject.ui.i.IDetailActivity;
import com.tencent.miniproject.utils.GsonUtil;
import com.tencent.miniproject.utils.OkHttpResultCallback;
import com.tencent.miniproject.utils.OkHttpUtil;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

/**
 * Created by PC on 2018/5/31.
 */

public class DetailPresenter implements IDetailPresenter{
    private static final String TAG = "DetailPresenter";
    private IDetailActivity mIDetailActivity;

    public DetailPresenter(IDetailActivity iDetailActivity) {
        mIDetailActivity = iDetailActivity;
    }

    @Override
    public void getDetail(Integer id, Double longitude, Double latitude) {
        String url = Constant.BASEURL+"post/getPostDetails?id="+id+"&longitude="+longitude+"&latitude="+latitude;
        OkHttpUtil.getInstance().getAsync(url, new OkHttpResultCallback() {
            @Override
            public void onError(Call call, Exception e) {
                mIDetailActivity.getDetailError(e.getMessage());
            }

            @Override
            public void onResponse(byte[] bytes) {
                try {
                    String msg = new String(bytes,"utf-8");
                    Log.e(TAG, "onResponse: " + msg);
                    DetailBeen detailBeen = GsonUtil.gsonToBean(msg,DetailBeen.class);
                    if (detailBeen.getResult().equals("success")){
                        mIDetailActivity.getDetailSuccess(detailBeen.getMessage());
                        Log.e(TAG, "onResponse: success" );
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void onClickVote(Integer id, boolean vote) {
        String url = Constant.BASEURL;
        if (vote){
            url += "vote/vote";
        }else {
            url += "vote/cancelVote";
        }
        Log.e(TAG, "onClickVote: url: " + url);
        Map<String,String> map = new HashMap<>();
        map.put("postId",id+"");
        OkHttpUtil.getInstance().postAsync(url, new OkHttpResultCallback() {
            @Override
            public void onError(Call call, Exception e) {
                Log.e(TAG, "onError: " + e.getMessage());
                mIDetailActivity.onClickVoteResult(false);
            }

            @Override
            public void onResponse(byte[] bytes) {
                try {
                    String msg = new String(bytes,"utf-8");
                    Log.e(TAG, "onResponse: " + msg );
                    if (msg.contains("success")){
                        mIDetailActivity.onClickVoteResult(true);
                    }else {
                        mIDetailActivity.onClickVoteResult(false);
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        },map);
    }

    @Override
    public void postComment(Integer id, String content) {
        String url = Constant.BASEURL+"comment/comment";
        Map<String,String> map = new HashMap<>();
        map.put("postId",id+"");
        map.put("content",content);
        OkHttpUtil.getInstance().postAsync(url, new OkHttpResultCallback() {
            @Override
            public void onError(Call call, Exception e) {
                Log.e(TAG, "onError: " + e.getMessage() );
                mIDetailActivity.postCommentResult(false);
            }

            @Override
            public void onResponse(byte[] bytes) {
                try {
                    String msg = new String(bytes,"utf-8");
                    Log.e(TAG, "onResponse: " + msg);
                    if (msg.contains("success")){
                        mIDetailActivity.postCommentResult(true);
                    }else {
                        mIDetailActivity.postCommentResult(false);
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        },map);

    }
}
